//
//  HMComposeCollectionController.swift
//  webo
//
//  Created by apple on 2016/11/22.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
let maxImgCount = 2

class HMComposeCollectionController: UICollectionViewController{

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = #colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1)
        self.collectionView!.register(HMComposeCollectCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }
    lazy var selectedimgArr:[UIImage] = [UIImage]()
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = selectedimgArr.count == maxImgCount ? 0 : 1
        
        return selectedimgArr.count + count
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! HMComposeCollectCell
        cell.delegate = self
        cell.backgroundColor = #colorLiteral(red: 0.5810584426, green: 0.1285524964, blue: 0.5745313764, alpha: 1)
        if selectedimgArr.count == indexPath.item {
        cell.img = nil
            return cell
        }
        cell.img = selectedimgArr[indexPath.item]
        return cell
    }

    
}


//UIImagePickerControllerDelegate
extension HMComposeCollectionController: UIImagePickerControllerDelegate,UINavigationControllerDelegate{

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//     print(info)
        let img = info[UIImagePickerControllerOriginalImage] as! UIImage
//        selectedimgArr.append(img as! UIImage)comColwidth
        selectedimgArr.append(img.dealScaleImg(width:ScreenWidth))
        picker.dismiss(animated: true, completion:{() -> () in
        
        })
        self.collectionView?.reloadData()
    }

}
///Users/apple/Desktop/webbo/webo/webo/Classes/View/Compose/Controller/HMComposeCollectionController.swift:49:30: Cannot assign value of type 'HMComposeCollectionController' to type '(UIImagePickerControllerDelegate & UINavigationControllerDelegate)?'
// MARK: - 遵守cell执行代理协议
extension HMComposeCollectionController:HMComposeCollectCellDelegate{
    func addBtnIsClicked(cell: HMComposeCollectCell?) {

        let imgpickVC = UIImagePickerController()
//        imgpickVC.allowsEditing = true
        imgpickVC.delegate = self
        self.present(imgpickVC, animated: true, completion: nil)
    }
    func removeBtnIsClicked(cell: HMComposeCollectCell) {
      print(remove)
        guard let index = collectionView?.indexPath(for: cell) else {
            return
        }
        selectedimgArr.remove(at: index.item)
        self.view.isHidden = selectedimgArr.count == 0
        collectionView?.reloadData()
    }
    
}
// MARK: - cell代理协议
@objc protocol HMComposeCollectCellDelegate {
  @objc optional func addBtnIsClicked(cell: HMComposeCollectCell?)
  @objc optional func removeBtnIsClicked(cell: HMComposeCollectCell)
    
}

// MARK: - collectioncell
class HMComposeCollectCell: UICollectionViewCell {
    //跳转到选择图片的控制器 cell中不能够完成跳转 应该将事件传递给 控制器
    //addClosure?()
    //调用代理方法
    //   设置代理
    weak var delegate:HMComposeCollectCellDelegate?

    //点击事件
    @objc private func clickAddBtn(){
        if img != nil {
            print("have img alerady")
            return
        }
        delegate?.addBtnIsClicked?(cell: self)
    }
    
    @objc private func clickRemoveBtn(){
        delegate?.removeBtnIsClicked?(cell: self)
    }
 
    override init(frame: CGRect) {
        super.init(frame: frame)
        setAddBtnAndRemoveBtn()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 懒加载
    var img:UIImage?{
        didSet{
        addBtn.setImage(img, for: .normal)
        removeBtn.isHidden = img == nil
        }
    }
    
    
    lazy var removeBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(#imageLiteral(resourceName: "compose_photo_close"), for: .normal)
//        btn.sizeToFit()
        return btn
    }()
    lazy var addBtn: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(#imageLiteral(resourceName: "compose_pic_add"), for: .normal)
//        btn.setImage(#imageLiteral(resourceName: "compose_pic_add_highlighted"), for: .highlighted)
        btn.imageView?.contentMode = .scaleAspectFill
        return btn
    }()
    func setAddBtnAndRemoveBtn(){
        self.contentView.addSubview(addBtn)
        self.contentView.addSubview(removeBtn)
        
        addBtn.snp.makeConstraints { (make) in
            make.edges.equalTo(self.contentView)
        }
        removeBtn.snp.makeConstraints { (make) in
            make.top.trailing.equalTo(self.contentView)
        }
//        removeBtn.isHidden = true
        
        addBtn.addTarget(self, action: #selector(clickAddBtn), for: .touchUpInside)
        removeBtn.addTarget(self, action: #selector(clickRemoveBtn), for: .touchUpInside)
    }
    
}




